package com.his.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.RecipelChild;
import com.his.mapper.RecipelChildMapper;
import com.his.service.RecipelChildService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (RecipelChild)表服务实现类
 *
 * @author makejava
 * @since 2020-10-23 18:14:39
 */
@Service("recipelChildService")
public class RecipelChildServiceImpl implements RecipelChildService {
    @Resource
    private RecipelChildMapper recipelChildMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Rsp queryById(Integer id) {
        RecipelChild recipelChild = this.recipelChildMapper.queryById(id);
        if (StringUtils.isEmpty(recipelChild)){
            return Rsp.fail().msg("没有当前对象");
        }else{
            return Rsp.success(recipelChild);
        }
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param recipelChild 实例对象
     * @return 查询的结果
     */
    @Override
    public Rsp queryAll(RecipelChild recipelChild) {
        List<RecipelChild> recipelChildList = this.recipelChildMapper.queryAll(recipelChild);
        return Rsp.success(recipelChildList);
    }

    /**
     * 新增数据
     *
     * @param recipelChild 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp insert(RecipelChild recipelChild) {
        recipelChild.setCreateTime(new Date());
        recipelChild.setIsDelete("0");
        int insert = this.recipelChildMapper.insert(recipelChild);
        if (insert>0){
            return Rsp.success(recipelChild).msg("添加成功");
        }else{
            return Rsp.fail();
        }
    }

    /**
     * 修改数据
     *
     * @param recipelChild 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp update(RecipelChild recipelChild) {
        int update = this.recipelChildMapper.update(recipelChild);
        if (update>0){
            return Rsp.success(recipelChild).msg("修改成功");
        }else{
            return Rsp.fail();
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Rsp deleteById(Integer id) {
        int deleteById = this.recipelChildMapper.deleteById(id);
        if (deleteById>0){
            return Rsp.success(null).msg("删除成功");
        }else {
            return Rsp.fail();
        }
    }

    /**
     * 逻辑删除子处方
     * @param id
     * @return
     */
    @Override
    public Rsp deleteByIdLogic(Integer id) {
        if( this.recipelChildMapper.deleteByIdLogic(id) > 0){
            return Rsp.success(null).msg("删除成功");
        }else{
            return Rsp.fail();
        }
    }

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @Override
    public PageInfo<RecipelChild> searchRecipelChildPage(PageParam<RecipelChild> pageParam) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            return new PageInfo<RecipelChild>(recipelChildMapper.superSearch(pageParam.getSuperSearchKeyWord()));
        }
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        //排序
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        return new PageInfo<RecipelChild>(recipelChildMapper.queryAllByModel(pageParam.getModel()));
    }
}
